﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Web;

namespace Viettel.VOFFICE.Web.Models.CustomAttributes
{
    public class FileSizeAttribute : ValidationAttribute
    {
        private readonly int _maxSize;
        public FileSizeAttribute(int max)
        {
            this._maxSize = max;
        }
        /// <summary>
        /// Kiểm tra dung lượng file gửi lên
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public override bool IsValid(object value)
        {
            if (value == null) return true;
            var postedFile = value as Stream;
            if (postedFile == null)
            {
                return true;
            }
            else
            {
                return _maxSize >= postedFile.Length;
            }
        }
        /// <summary>
        /// Xuất thông báo lỗi
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public override string FormatErrorMessage(string name)
        {
            return string.Format(@Resources.Resources.FileSizeError, _maxSize / 1024);
        }
    }
}